package com.tencent.cloud.uikit.widget.dialog;

import android.content.Context;
import android.support.v7.app.AppCompatDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.tencent.cloud.uikit.R;

/**
 * @author vinsonhuang
 * @date 2018/10/12
 */
public class BottomSheetDialog extends AppCompatDialog {

    private Button mTakePhotoBtn;

    private Button mPhotoAlbumBtn;

    private Button mCancelBtn;

    public BottomSheetDialog(Context context) {
        this(context, R.style.ActionSheetDialogStyle);
        init(context);
    }

    public BottomSheetDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    public BottomSheetDialog(Context context, boolean cancelable,
                             OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    public void setClickListener(View.OnClickListener listener) {
        mTakePhotoBtn.setOnClickListener(listener);
        mPhotoAlbumBtn.setOnClickListener(listener);
        mCancelBtn.setOnClickListener(listener);
    }

    private void init(Context context) {
        //填充对话框的布局
        View inflate = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_dialog_layout,
                null);
        //初始化控件
        mTakePhotoBtn = inflate.findViewById(R.id.btn_take_photo);
        mPhotoAlbumBtn = inflate.findViewById(R.id.btn_photo_album);
        mCancelBtn = inflate.findViewById(R.id.btn_cancel);
        //将布局设置给Dialog
        setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        if (dialogWindow == null) {
            return;
        }
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置Dialog距离底部的距离
//        lp.y = 20;
        //将属性设置给窗体
        dialogWindow.setAttributes(lp);
    }
}
